import { PermissionsAndroid } from 'react-native';
import ApplyPermissionConfig from "../config/ApplyPermissionConfig";
import ToastUtil from "../utils/toast";


class ApplyPermission {
	//检测权限
	_checkPermission = async (permissionKey) => {
		let checkResult = false;
		try {
			const granted = PermissionsAndroid.check(permissionKey);
			await granted.then((data) => {
				console.log("检测权限", data);
				checkResult = data;
			}).catch((err) => {
				console.log("检测权限失败", err.toString());
			})
		} catch (err) {
			console.log("检测权限失败-err", err.toString());
		}
		console.log("最后结果", checkResult);
		return checkResult;
	}
	//授权弹框
	/* 
	permissionKey: 权限
	messageData :{
					//第一次请求拒绝后提示用户你为什么要这个权限
					'title': '我要读写权限',
					'message': '没权限我不能工作，同意就好了'
	} */
	_accredit = async (permissionKey, messageData) => {
		try {
			//返回string类型
			const granted = await PermissionsAndroid.request(permissionKey, messageData);
			if (granted === PermissionsAndroid.RESULTS.GRANTED) {
				console.log("你已获取了读写权限");
				return Promise.resolve({ data: 1 });
			} else {
				console.log("权限失败,请手动授权");
				return Promise.resolve({ data: 0 });
			}
		} catch (err) {
			console.log("err.toString()" + err.toString());
			return Promise.resolve({ data: 0 });
		}
	}

	_requestMultiplePermission = async () => {
		try {
			const permissions = Object.values(ApplyPermissionConfig) || [];
			//返回得是对象类型
			const granteds = await PermissionsAndroid.requestMultiple(permissions);
			console.log("granteds", granteds);
			if ((granteds && Object.values(granteds).length) == permissions.length) {
				ToastUtil.showSuccess("权限设置成功");
			} else {
				showError.showSuccess("权限设置失败");
			}

		} catch (err) {
			console.log(err.toString());
		}
	}
}

module.exports = new ApplyPermission();